package com.adee.gulimall.product.app;

import com.adee.gulimall.common.utils.PageUtils;
import com.adee.gulimall.util.utils.R;
import com.adee.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.adee.gulimall.product.entity.AttrEntity;
import com.adee.gulimall.product.entity.AttrGroupEntity;
import com.adee.gulimall.product.service.AttrAttrgroupRelationService;
import com.adee.gulimall.product.service.AttrGroupService;
import com.adee.gulimall.product.service.AttrService;
import com.adee.gulimall.product.service.CategoryService;
import com.adee.gulimall.product.vo.AttrGroupRelationReqVo;
import com.adee.gulimall.product.vo.AttrGroupRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author adee
 * @email 609980188@qq.com
 * @date 2022-08-19 16:28:49
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable Long categoryId){
        PageUtils page = attrGroupService.queryPage(params, categoryId);

        return R.ok().put("page", page);
    }

    // 保存关联信息
    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody AttrAttrgroupRelationEntity[] relas) {
//        AttrAttrgroupRelationEntity rela = new AttrAttrgroupRelationEntity();
//        rela.setAttrId(vo.getAttrId());
//        rela.setAttrGroupId(vo.getAttrGroupId());
        relationService.saveBatch(Arrays.asList(relas));
        return R.ok();
    }

    // 查询关联属性列表
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> list = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", list);
    }

    // 获取当前分类中没有被其他分组关联的属性
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R attrRelationNo(
            @PathVariable("attrGroupId") Long attrGroupId,
            @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getRelationAttrNo(attrGroupId, params);
        return R.ok().put("page", page);
    }

    // 查询某个分类下所有分组及分组下所有属性
    @GetMapping("/{catelogId}/withattr")
    public R withAttr( @PathVariable("catelogId") Long catelogId ) {
        List<AttrGroupRespVo> list = attrService.withattr(catelogId);
        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogPath(categoryService.getCategoryPath(attrGroup.getCatelogId()));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @RequestMapping("/attr/relation/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R deleteRelation(@RequestBody AttrGroupRelationReqVo[] attrList){
        // attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        attrService.deleteRelation(attrList);
        return R.ok();
    }

}
